package com.eagle.coders.core.exceptions;

import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

//import my.com.infopro.icba10.kernel.base.delegate.BaseDto;
//import my.com.infopro.icba10.kernel.exceptionhandling.config.KernCoreConst;
//import my.com.infopro.icba10.kernel.exceptionhandling.config.SystemErr;
//import my.com.infopro.icba10.kernel.exceptionhandling.handler.ExceptionHandlerFactory;

import org.apache.log4j.Logger;

import com.eagle.coders.core.exceptions.handler.ExceptionHandler;

public class ExceptionInterceptor {

    private static final Logger Log = Logger.getLogger(ExceptionInterceptor.class.getName());
    private ExceptionHandler handler = null;

    @AroundInvoke
    public Object captureException(InvocationContext invocationContext) throws Exception {
        if (Log.isDebugEnabled()) {
            Log.debug("=========== in the ExceptionInterceptor ===========");
            Log.debug("Method intercepted: " + invocationContext.getTarget() + "."
                    + invocationContext.getMethod().getName());
        }
        
        if(null == invocationContext.getParameters()){
            Log.debug("=========== in the ExceptionInterceptor: Parameters ===========");
            return invocationContext.proceed();
        }
        
        Object param = invocationContext.getParameters()[0];
        
//        if (!(param instanceof BaseDto)) {
//            Log.debug("First parameter not instance of BaseDto");
//            return invocationContext.proceed();
//        }
//
//        try {
//            return invocationContext.proceed();
//        } catch (AppAlertException aae) {
//            // call the alert service here
//            throw aae;
//        } catch (BaseSystemException se) {
//            getExceptionHandler().handle(Thread.currentThread(), se);
//            // call the alert service here
//            throw se;
//        } catch (RuntimeException re) {
//            BaseSystemException se = null;
//            if (isLoopPersistence(re)) {
//                se = new PersistenceException(
//                        "Please contact SA. RuntimeException caught in ExceptionInterceptor", re,
//                        KernCoreConst.getInstance().getSystemErrMsgLoc(), "BaseSystemException",
//                        SystemErr.SYS0008.getValue(), new Object[] {});
//            } else {
//                se = new BaseSystemException(
//                        "Please contact SA. RuntimeException caught in ExceptionInterceptor", re,
//                        KernCoreConst.getInstance().getSystemErrMsgLoc(), "BaseSystemException",
//                        SystemErr.SYS0002.getValue(), new Object[] {});
//            }

//            getExceptionHandler().handle(Thread.currentThread(), se);
            // call the alert service here
//            throw se;
        throw null; //TODO: this is for change in future... wat a stupid chage i did======= kekeke
//        } catch (Error e) {
//            BaseSystemException se = new BaseSystemException(
//                    "Please contact SA. Error caught in ExceptionInterceptor", e, 
//                    KernCoreConst.getInstance().getSystemErrMsgLoc(), "BaseSystemException",
//                    SystemErr.SYS0003.getValue(), new Object[] {});
//            getExceptionHandler().handle(Thread.currentThread(), se);
//            // call the alert service here
//            throw se;
//        }
    }

//    private ExceptionHandler getExceptionHandler() {
//        if (null == handler) {
//            handler = ExceptionHandlerFactory.getInstance().getEjbExceptionHandler();
//        }
//        return handler;
//    }

    private boolean isLoopPersistence(Throwable throwable) {
        Log.debug(throwable.getClass().getName());

        if (!(throwable instanceof javax.persistence.PersistenceException)) {
            Throwable causeThrowable = throwable.getCause();
            if (null == causeThrowable) {
                return false;
            }
            return isLoopPersistence(causeThrowable);
        }
        return true;
    }
}
